@echo off
setlocal enabledelayedexpansion

REM MongoShake 服务启动脚本 (Windows版本)
REM 用于管理独立的MongoDB和MongoShake服务

set "RED=[91m"
set "GREEN=[92m"
set "YELLOW=[93m"
set "NC=[0m"

REM 打印带颜色的消息
:print_message
echo %GREEN%[INFO]%NC% %~1
goto :eof

:print_warning
echo %YELLOW%[WARNING]%NC% %~1
goto :eof

:print_error
echo %RED%[ERROR]%NC% %~1
goto :eof

REM 检查Docker是否运行
:check_docker
docker info >nul 2>&1
if errorlevel 1 (
    call :print_error "Docker is not running. Please start Docker first."
    exit /b 1
)
goto :eof

REM 启动MongoDB服务
:start_mongodb
call :print_message "Starting MongoDB service..."
docker-compose -f docker-compose.mongodb.yml up -d
if errorlevel 1 (
    call :print_error "Failed to start MongoDB service"
    exit /b 1
)

call :print_message "Waiting for MongoDB to be ready..."
REM 等待MongoDB健康检查通过
set timeout=120
set counter=0
:wait_loop
if %counter% geq %timeout% (
    call :print_error "MongoDB failed to start within %timeout% seconds"
    exit /b 1
)

docker-compose -f docker-compose.mongodb.yml ps | findstr "healthy" >nul
if not errorlevel 1 (
    call :print_message "MongoDB is ready!"
    goto :eof
)

timeout /t 2 /nobreak >nul
set /a counter+=2
echo -n .
goto :wait_loop

REM 启动MongoShake服务
:start_mongoshake
call :print_message "Starting MongoShake service..."
docker-compose -f docker-compose.mongoshake.yml up -d
if errorlevel 1 (
    call :print_error "Failed to start MongoShake service"
    exit /b 1
)
call :print_message "MongoShake service started!"
goto :eof

REM 停止所有服务
:stop_all
call :print_message "Stopping all services..."
docker-compose -f docker-compose.mongoshake.yml down
docker-compose -f docker-compose.mongodb.yml down
call :print_message "All services stopped!"
goto :eof

REM 查看服务状态
:status
call :print_message "MongoDB service status:"
docker-compose -f docker-compose.mongodb.yml ps

echo.
call :print_message "MongoShake service status:"
docker-compose -f docker-compose.mongoshake.yml ps
goto :eof

REM 查看日志
:logs
if "%1"=="mongodb" (
    call :print_message "MongoDB logs:"
    docker-compose -f docker-compose.mongodb.yml logs -f
) else if "%1"=="mongoshake" (
    call :print_message "MongoShake logs:"
    docker-compose -f docker-compose.mongoshake.yml logs -f
) else (
    call :print_message "All logs:"
    start "MongoDB Logs" cmd /c "docker-compose -f docker-compose.mongodb.yml logs -f"
    docker-compose -f docker-compose.mongoshake.yml logs -f
)
goto :eof

REM 清理所有数据
:clean
call :print_warning "This will remove all data volumes. Are you sure? (y/N)"
set /p response=
if /i "%response%"=="y" (
    call :print_message "Cleaning all data..."
    docker-compose -f docker-compose.mongoshake.yml down -v
    docker-compose -f docker-compose.mongodb.yml down -v
    docker network rm mongodb_03-mongoshake_mongodb-network 2>nul
    call :print_message "All data cleaned!"
) else (
    call :print_message "Clean operation cancelled."
)
goto :eof

REM 显示帮助信息
:show_help
echo Usage: %0 [COMMAND]
echo.
echo Commands:
echo   start       Start MongoDB and MongoShake services
echo   stop        Stop all services
echo   restart     Restart all services
echo   status      Show service status
echo   logs        Show logs (mongodb^|mongoshake^)
echo   clean       Remove all data volumes
echo   help        Show this help message
echo.
echo Examples:
echo   %0 start                    # Start all services
echo   %0 logs mongoshake          # Show MongoShake logs
echo   %0 status                   # Show all service status
goto :eof

REM 主函数
:main
call :check_docker
if errorlevel 1 exit /b 1

if "%1"=="" goto :start
if "%1"=="start" goto :start
if "%1"=="stop" goto :stop
if "%1"=="restart" goto :restart
if "%1"=="status" goto :status
if "%1"=="logs" goto :logs
if "%1"=="clean" goto :clean
if "%1"=="help" goto :show_help
if "%1"=="--help" goto :show_help
if "%1"=="-h" goto :show_help

call :print_error "Unknown command: %1"
call :show_help
exit /b 1

:start
call :start_mongodb
if errorlevel 1 exit /b 1
call :start_mongoshake
if errorlevel 1 exit /b 1
call :print_message "All services started successfully!"
goto :eof

:stop
call :stop_all
goto :eof

:restart
call :stop_all
timeout /t 2 /nobreak >nul
call :start_mongodb
if errorlevel 1 exit /b 1
call :start_mongoshake
if errorlevel 1 exit /b 1
call :print_message "All services restarted successfully!"
goto :eof

:logs
call :logs %2
goto :eof

:clean
call :clean
goto :eof

REM 执行主函数
call :main %* 